﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL.Wrapper;
using System.Collections.Generic;
using DAL;

public partial class Query : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }
        TypeWrapper wrapper = new TypeWrapper();
        List<string> list = new List<string>();
        list = wrapper.getSubName("后台管理");
        Typelist.DataSource = list;
        list.Add("不选");
        Typelist.DataBind();
        list.Clear();
        list = wrapper.getSubName(Typelist.SelectedValue);
        TypeClass.DataSource = list;
        TypeClass.DataBind();
    }

    protected void QueryClick(object sender, EventArgs e)
    {
        string name = GoodsNameInput.Text;
        string type = null;
        string sql = null;
        GoodsWrapper wrapper = new GoodsWrapper();
        HouseDiyHelper helper = HouseDiyHelper.GetInstance();
        DataTable table = new DataTable();
        if (TypeClass.SelectedValue.Length < 1)
        {
            sql = "select * from Goods left join Brand on Goods.brandID = Brand.brandID left join dbo.Type on dbo.Type.typeID=Goods.typeID where goodsName = '" + name + "'";
            table = helper.getResultBySql(sql);
        }

        else 
        {
            type = TypeClass.SelectedValue;
            sql = "select * from Goods left join Brand on Goods.brandID = Brand.brandID left join dbo.Type on dbo.Type.typeID=Goods.typeID where Type.typeName = '" + type + "' and goodsName = '" + name + "'";
            table = helper.getResultBySql(sql);
        }
        Goods.DataSource = table;
        Goods.DataBind(); 
    }

    protected void TypeSelecetChanged(object sender, EventArgs e)
    {
        string name = Typelist.SelectedValue;
        TypeWrapper wrapper = new TypeWrapper();
        List<string> list = new List<string>();
        list = wrapper.getSubName(name);
        TypeClass.DataSource = list;
        TypeClass.DataBind();
    }

    protected void ClassSelecetChanged(object sender, EventArgs e)
    {
        string name = TypeClass.SelectedValue;
        DataTable table = new DataTable();
        GoodsWrapper wrapper = new GoodsWrapper();
        table = wrapper.getResultByType(name);
        Goods.DataSource = table;
        Goods.DataBind();
    }

    protected void RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(Goods.DataKeys[e.RowIndex].Value);
        GoodsWrapper wrapper = new GoodsWrapper();
        wrapper.deleteByID(id);
        string name = TypeClass.SelectedValue;
        DataTable table = new DataTable();
        table = wrapper.getResultByType(name);
        Goods.DataSource = table;
        Goods.DataBind();
    }

    protected void RowUpdate(object sender, GridViewCommandEventArgs e)
    {
        string cmd = e.CommandName; //获得name
        int Id = Convert.ToInt32(e.CommandArgument);
        GoodsWrapper wrapper = new GoodsWrapper();
        //wrapper.getGoodsByID(Id);
        //显示详细信息: 品牌，图片，模型....
    }

    
}
